package net.sf.sbv.modals;


import java.math.BigInteger;
import java.util.Date;

/**
 *
 * @author Pratik
 */
public class Message {
    public static final int RECEIVED = 1, SENT = 2;

    private Contact contact;
    private String messageText;
    private int messageType;
    private Date dateStamp;

    public Message(BigInteger date, String body, int type) {
        messageType = type;
        messageText = body;
        dateStamp = new Date(date.longValue());
    }

    public void setMessageText(String input) {
        messageText = input;
    }

    public String getMessageText() {
        return messageText;
    }

    public String getMessageHtmlText() {
        StringBuilder sb = new StringBuilder("<html><body>");
        sb.append(getConvertedBRTagText());
        sb.append("</body></html>");

        return sb.toString();
    }

    public String getConvertedBRTagText() {
        StringBuilder sb = new StringBuilder("");

        boolean firstLine = true;
        for(String line : messageText.split("\n")) {
            if(firstLine) {
                firstLine = false;
            } else {
                sb.append("<br/>");
            }
            sb.append(line);
        }
        return sb.toString();
    }

    public void setMessageType(int input) {
        messageType = input;
    }

    public int getMessageType() {
        return messageType;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    @Override
    public String toString() {
        String tempString;
        tempString = getSentOrReceived();
        tempString += dateStamp.toString() + ":  ";
        tempString += messageText;
        return tempString;
    }

    public String getTimeStamp() {
        return getSentOrReceived() + " on " + dateStamp.toString();
    }

    public String getSentOrReceived() {
        if (messageType == SENT) {
            return "Sent";
        } else {
            return "Received";
        }
    }
}
